/*
 * Copyright (C) 2006-2015
 * 版权所有者为北京捷越联合信息咨询有限公司。本系统是商用软件,未经授权擅自复制或传播本程序的部分或全部将是非法的。
 * @title: ChangeModeFilter.java
 * @package com.jy.modules.cims.component.creditRight.plan
 * @author zhaox
 * @date 2015-3-5 下午4:38:31
 * @version v1.00
 * @description: TODO(用一句话描述该文件做什么)
 */

package com.jy.modules.cims.component.creditRight.plan;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.exception.CimsException;
import com.jy.modules.cims.component.credit.credit.CreditFeeImpl;
import com.jy.modules.cims.data.dao.RepaymentPlanDao;
import com.jy.modules.cims.data.dto.credit.PlanChangeDTO;

/**
 * @classname: AdvanceFinishModeFilter
 * @description: TODO(这里用一句话描述这个类的作用)
 */
@Service("com.jy.modules.cims.component.creditRight.plan.AdvanceFinishModeFilter")
public class AdvanceFinishModeFilter implements DealModeFilter {

    Logger logger = LoggerFactory.getLogger(AdvanceFinishModeFilter.class);

    @Autowired
    private RepaymentPlanDao repaymentPlanDao;

    @Autowired
    @Qualifier("com.jy.modules.cims.component.credit.credit.CreditFeeImpl")
    CreditFeeImpl creditFeeImpl;

    /* (非 Javadoc)
    * <p>title: doFilter</p>
    * <p>description: </p>
    * @param object
    * @param dealModeChain
    * @see com.jy.modules.cims.component.creditRight.plan.DealModeFilter#doFilter(java.lang.Object, com.jy.modules.cims.component.creditRight.plan.DealModeChain)
    */

    @Override
    public void doFilter(int index, PlanChangeDTO planChangeDTO, DealModeChain dealModeChain) {
        // 通过合约判断是不是自己处理该类型
        String creditCode = planChangeDTO.getContractNo();
        String transferId = planChangeDTO.getTransferId();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("creditCode", creditCode);
        paramMap.put("transferId", transferId);
        // 通过合同号（债券编号）查询合约下约定的处理方式。没有查询到抛异常
        List<Map> list = repaymentPlanDao.queryPlanContractDealMode(paramMap);
        Map dealModeMap = new HashMap();
        String contractCode = "";
        for (Map map : list) {
            dealModeMap.put(map.get("TYPE"), map.get("DISPOSE_TYPE"));
            contractCode = map.get("CONTRACT_CODE").toString();
        }
        // 还款计划变更类型
        String changeType = planChangeDTO.getChangeType();
        Object disposeType = dealModeMap.get(changeType);
        // disposeType = "01";
        if (disposeType == null) {
            throw new CimsException("未配置该合同的违约信息！" + creditCode + "," + transferId);
        }
        // 处理方式中，提前结清和全额匹配逻辑相同
        if (CreditPlanConstant.DEAL_MODE_ADVANCEFINISH.equals(disposeType.toString())
                || CreditPlanConstant.DEAL_MODE_ALLMATCH.equals(disposeType.toString())) {// 逾期>提前结清
            // 处理逻辑
            logger.info("处理类型为：提前结清");
            logger.info(planChangeDTO.getPayoffDate());
            if (planChangeDTO.getPayoffDate() != null) {/*
                paramMap.put("currentDate", planChangeDTO.getPayoffDate());
                Map statusMap = repaymentPlanDao.selectCurrentTimeAndStatus(paramMap);
                int term = Integer.valueOf(statusMap.get("TERM").toString()).intValue();
                //当前期数提前结清必须和核心一致，否则两系统不一致，无法结算。机构系统当正常结算，核心当提前结算，直接验证不能通过。
                //如果两边状态不一致，数据不能过去，只能手动处理，修复程序。
                String planStatus = statusMap.get("PLAN_STATUS") == null ? "" : statusMap.get("PLAN_STATUS").toString();
                if ("01".equals(planStatus)) {
                    term = term + 1;
                }
                paramMap.put("currentTime", term);
                repaymentPlanDao.updateAdvanceRepaymentPlans(paramMap);// 更新贷款计划
                repaymentPlanDao.updateCostPlans(paramMap);// 更新结算计划
                // paramMap.put("creditStatus", CimsConstant.CREDIT_STATUS_END);
                // repaymentPlanDao.updateCreditFlag(paramMap);// 更新债权结束标志
                paramMap.put("planStatus", changeType);
                repaymentPlanDao.updatePlanChanged(paramMap);// 更新该期还款计划的状态
                paramMap.put("creditStatus", changeType);
                CreditFeeParamDto creditFeeParamDto = new CreditFeeParamDto();
                creditFeeParamDto.setContractCode(contractCode);
                creditFeeParamDto.setCurrentTime(new Long(term));
                creditFeeParamDto.setSettleStatus(CreditPlanConstant.DEAL_MODE_ADVANCEFINISH);
                creditFeeParamDto.setTransferId(transferId);
                CreditFeeDto creditFeeDto = creditFeeImpl.getAbnormalSettleInfo(creditFeeParamDto);
                paramMap.put("mustAmount", creditFeeDto.getMustAmount());// 应还本
                paramMap.put("matchAmount", creditFeeDto.getMatchAmount());
                paramMap.put("returnAmount", creditFeeDto.getReturnAmount());
                repaymentPlanDao.updateCostChanged(paramMap);// 更新该期结算计划的状态
            */}
        } else {
            index++;
            dealModeChain.doFilter(index, planChangeDTO, dealModeChain);
        }
    }
}
